package ltd.newbee.mall.util.paypal;

import com.paypal.core.PayPalHttpClient;
import com.paypal.http.HttpResponse;
import com.paypal.payments.CapturesRefundRequest;
import com.paypal.payments.Money;
import com.paypal.payments.Refund;
import com.paypal.payments.RefundRequest;

import java.io.IOException;

public class RefundOrder implements OrderAbstractHandler {

    private final String CURRENCY_CODE = "USD";

    private String refundmoney = "0.00";

    @Override
    public RefundRequest buildRequestBody() {
        RefundRequest refundRequest = new RefundRequest();
        Money money = new Money();
        money.currencyCode(CURRENCY_CODE);
        money.value(refundmoney);
        refundRequest.amount(money);
        return refundRequest;
    }

    /**
     *
     * @param id 扣款id,从订单扣款对象中获得
     * @param client
     * @return
     * @throws IOException
     */
    @Override
    public HttpResponse<Refund> getResponseMessage(String id, PayPalHttpClient client) throws IOException {
        CapturesRefundRequest request = new CapturesRefundRequest(id);
        request.prefer("return=representation");
        request.requestBody(buildRequestBody());
        return client.execute(request);
    }

    public void setRefundmoney(String refundmoney) {
        this.refundmoney = refundmoney;
    }
}
